package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.LikeAdminThreadLocal;
import com.mdd.admin.service.IAnswerContractService;
import com.mdd.admin.validate.AnswerContractCreateValidate;
import com.mdd.admin.validate.AnswerContractSearchValidate;
import com.mdd.admin.validate.AnswerContractUpdateValidate;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.vo.AnswerContractDetailVo;
import com.mdd.admin.vo.AnswerContractListedVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.AnswerContract;
import com.mdd.common.mapper.AnswerContractMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 新建合同回款信息实现类
 * @author LikeAdmin
 */
@Service
public class AnswerContractServiceImpl implements IAnswerContractService {
        
    @Resource
    AnswerContractMapper answerContractMapper;


    /**
     * 新建合同回款信息列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<AnswerContractListedVo>
     */
    @Override
    public PageResult<AnswerContractListedVo> list(PageValidate pageValidate, AnswerContractSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<AnswerContract> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("id");

        answerContractMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:creator:str",
            "=:servicer:str",
            "=:contractId@contract_id:str",
            "like:contractName@contract_name:str",
            "like:customerName@customer_name:str",
            "like:caseName@case_name:str",
            "=:agencyFee@agency_fee:str",
            "=:contractValue@contract_value:str",
            "=:currency:str",
            "=:paymentCurrency@payment_currency:str",
            "=:officialFee@official_fee:str",
            "=:currencyid@currencyId:long",
            "=:paymentCurrencyid@payment_currencyId:long",
            "=:remarks:str",
            "lte:topUrgent@top_urgent:str",
            "=:planTopUrgent@plan_top_urgent:str",
            "=:amountCashed@amount_cashed:str",
            "=:paymentType@payment_type:str",
            "=:collectionAgentDate@collection_agent_date:str",
            "=:collectingAgencyFees@collecting_agency_fees:str",
            "=:collectingCompanyAccount@collecting_company_account:str",
            "=:invoiceAmount@invoice_amount:str",
            "=:timeBilling@time_billing:str",
            "=:paymentMethod@payment_method:str",
            "=:consolidatedCollection@consolidated_collection:str",
            "=:invoiceNumber@invoice_number:str",
            "=:payee:str",
            "=:collectingCompanyAccountid@collecting_company_accountId:long",
        });

        IPage<AnswerContract> iPage = answerContractMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<AnswerContractListedVo> list = new LinkedList<>();
        for(AnswerContract item : iPage.getRecords()) {
            AnswerContractListedVo vo = new AnswerContractListedVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 新建合同回款信息详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return AnswerContract
     */
    @Override
    public AnswerContractDetailVo detail(Integer id) {
        AnswerContract model = answerContractMapper.selectOne(
                new QueryWrapper<AnswerContract>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        AnswerContractDetailVo vo = new AnswerContractDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 新建合同回款信息新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(AnswerContractCreateValidate createValidate) {
        AnswerContract model = new AnswerContract();

        model.setCreator(createValidate.getCreator());
        model.setServicer(createValidate.getServicer());
        model.setContractId(createValidate.getContractId());
        model.setContractName(createValidate.getContractName());
        model.setCustomerName(createValidate.getCustomerName());
        model.setCaseName(createValidate.getCaseName());
        model.setAgencyFee(createValidate.getAgencyFee());
        model.setContractValue(createValidate.getContractValue());
        model.setCurrency(createValidate.getCurrency());
        model.setPaymentCurrency(createValidate.getPaymentCurrency());
        model.setOfficialFee(createValidate.getOfficialFee());
        model.setCurrencyid(createValidate.getCurrencyid());
        model.setPaymentCurrencyid(createValidate.getPaymentCurrencyid());
        model.setRemarks(createValidate.getRemarks());
        model.setTopUrgent(createValidate.getTopUrgent());
        model.setPlanTopUrgent(createValidate.getPlanTopUrgent());
        model.setAmountCashed(createValidate.getAmountCashed());
        model.setPaymentType(createValidate.getPaymentType());
        model.setCollectionAgentDate(createValidate.getCollectionAgentDate());
        model.setCollectingAgencyFees(createValidate.getCollectingAgencyFees());
        model.setCollectingCompanyAccount(createValidate.getCollectingCompanyAccount());
        model.setInvoiceAmount(createValidate.getInvoiceAmount());
        model.setTimeBilling(createValidate.getTimeBilling());
        model.setPaymentMethod(createValidate.getPaymentMethod());
        model.setConsolidatedCollection(createValidate.getConsolidatedCollection());
        model.setInvoiceNumber(createValidate.getInvoiceNumber());
        model.setPayee(createValidate.getPayee());
        model.setCollectingCompanyAccountid(createValidate.getCollectingCompanyAccountid());

        // 设置 操作人信息 以及 创建时间
        model.setUserId(Long.valueOf(LikeAdminThreadLocal.getAdminId()));
        model.setUserName(LikeAdminThreadLocal.get("username").toString());
        model.setCreateTime(String.valueOf(System.currentTimeMillis() / 1000));
        model.setUpdateTime(String.valueOf(System.currentTimeMillis() / 1000));
        model.setIsDelete(0);

        answerContractMapper.insert(model);
    }

    /**
     * 新建合同回款信息编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(AnswerContractUpdateValidate updateValidate) {
        AnswerContract model = answerContractMapper.selectOne(
                new QueryWrapper<AnswerContract>()
                    .eq("id",  updateValidate.getId())
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setCreator(updateValidate.getCreator());
        model.setServicer(updateValidate.getServicer());
        model.setContractId(updateValidate.getContractId());
        model.setContractName(updateValidate.getContractName());
        model.setCustomerName(updateValidate.getCustomerName());
        model.setCaseName(updateValidate.getCaseName());
        model.setAgencyFee(updateValidate.getAgencyFee());
        model.setContractValue(updateValidate.getContractValue());
        model.setCurrency(updateValidate.getCurrency());
        model.setPaymentCurrency(updateValidate.getPaymentCurrency());
        model.setOfficialFee(updateValidate.getOfficialFee());
        model.setCurrencyid(updateValidate.getCurrencyid());
        model.setPaymentCurrencyid(updateValidate.getPaymentCurrencyid());
        model.setRemarks(updateValidate.getRemarks());
        model.setTopUrgent(updateValidate.getTopUrgent());
        model.setPlanTopUrgent(updateValidate.getPlanTopUrgent());
        model.setAmountCashed(updateValidate.getAmountCashed());
        model.setPaymentType(updateValidate.getPaymentType());
        model.setCollectionAgentDate(updateValidate.getCollectionAgentDate());
        model.setCollectingAgencyFees(updateValidate.getCollectingAgencyFees());
        model.setCollectingCompanyAccount(updateValidate.getCollectingCompanyAccount());
        model.setInvoiceAmount(updateValidate.getInvoiceAmount());
        model.setTimeBilling(updateValidate.getTimeBilling());
        model.setPaymentMethod(updateValidate.getPaymentMethod());
        model.setConsolidatedCollection(updateValidate.getConsolidatedCollection());
        model.setInvoiceNumber(updateValidate.getInvoiceNumber());
        model.setPayee(updateValidate.getPayee());
        model.setCollectingCompanyAccountid(updateValidate.getCollectingCompanyAccountid());

        // 设置 操作人信息 以及 更新时间
        model.setUpdateUserId(Long.valueOf(LikeAdminThreadLocal.getAdminId()));
        model.setUpdateUserName(LikeAdminThreadLocal.get("username").toString());
        model.setUpdateTime(String.valueOf(System.currentTimeMillis() / 1000));

        answerContractMapper.updateById(model);
    }

    /**
     * 新建合同回款信息删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        AnswerContract model = answerContractMapper.selectOne(
                new QueryWrapper<AnswerContract>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setIsDelete(1);
        model.setDeleteTime(System.currentTimeMillis() / 1000);
        answerContractMapper.updateById(model);
    }

}
